package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.ValidateCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RequestMapping
@RestController
@Slf4j
@Api(tags = "用户端通用接口")
public class CommonController {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 手机号快速登陆发送验证码
     * @param telephone
     * @return
     */
    @ApiOperation("手机号快速登陆发送验证码")
    @PostMapping("/validatecode/send4Login")
    public Result send4Order(String telephone){
        log.info("手机号快速登陆发送验证码:{}",telephone);
        Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        //发送验证码给手机 由于阿里云短信服务申请不通过 遂省略该部分代码

        //将验证码存入redis
        redisTemplate.opsForValue().set(telephone,String.valueOf(validateCode),5, TimeUnit.MINUTES);
        //不支持时的存活时间设置
        //redisTemplate.expire("validateCode", 5, TimeUnit.MINUTES);
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS,null);
    }
}
